﻿using System;
using Commons;
using DAL;
using DAL.Enums;
using NUnit.Framework;
using SimpComm;
using ViewModel;
using log4net;
using System.Collections.Generic;

namespace Bll
{
    public class UserService : BaseService<User>
    {
        public MsgBox Insert(UserCreateModel addModel)
        {
            if (addModel.LoginID.IsNullOrWhiteSpace() || addModel.LoginPwd.IsNullOrWhiteSpace())
                return new MsgBox(false, "请输入密码.");
            var user = new User()
                           {
                               Security_LoginID = addModel.LoginID,
                               Security_LoginPwd = Util.UserEncode(addModel.LoginPwd),
                               Security_Email = addModel.Security_Email,
                               Name = addModel.Name,
                           };
            Action<User> defaultValueFun = p =>
                {
                    p.Wallet = 0.0m;
                };
            MsgBox msg = base.Insert(user, defaultValueFun);
            return msg;
        }


        public MsgBox Update(UserEditModel editModel)
        {
            var user = new User();
            user.Name = editModel.Name;
            MsgBox msg = base.Update(p => p.ID == editModel.ID, p => new User()
                {
                    Name = editModel.Name
                });
            return msg;
        }

        public DataInfo<UserEditModel> GetEditView(Func<User, bool> func)
        {
            Func<User, UserEditModel> select = p =>
                                                          {
                                                              return new UserEditModel()
                                                                         {
                                                                             Name = p.Name,
                                                                         };
                                                           };
            return base.GetModel(func, select);
        }

        public MD<UserListModel> GetMul_List(Func<User, bool> func)
        {
            var s = new SMP<User, UserListModel>()
                       {
                           getmodel = p =>
                                                         {
                                                             return new UserListModel()
                                                                        {
                                                                            ID = p.ID,
                                                                            Name = p.Name,
                                                                        };
                                                         },
                           selected = func,
                       };
            return base.SM(s);
        }

    }


#if DEBUG
    [TestFixture]
    public class TestUserService
    {
        public UserService UserService
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        [Test]
        public void Test_ChangePassword()
        {
            //var UserService = new UserService();
            ////取值
            //var data = UserService.GetEntity(p => p.ID == 1);
            //var _old = data.Security_LoginPwd;
            //var _new = Randoms.GetRandomPureChar(8);
            //Assert.IsTrue(UserService.ChangePassword(1, new UserRequest()
            //                                     {
            //                                         OldPassord = _old,
            //                                         NewPassword = _new
            //                                     }));
            //data = UserService.GetEntity(p => p.ID == 1);
            //Assert.AreEqual(data.Security_LoginPwd, _new);
        }
    }
#endif
}